package cn.school.thoughtworks.section1;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class PracticeB {
    List<String> collectSameElements(List<String> collection1, List<List<String>> collection2) {
     /*   //实现练习要求，并改写该行代码。
        //用Iterator遍历集合元素
        Iterator<List<String>> iterator = collection2.iterator();
        //创建一个新的集合
        List<List<String>> list1 = new ArrayList<>();
        //判断iterator迭代器的下一个元素是否存在
        while(iterator.hasNext()){
            //存在的话，添加到list1中.
            list1.add(iterator.next());
        }
*/


/*
        return collection1.stream().filter(item->list1.iterator().next().contains(item)).collect(Collectors.toList());*/
       List<String> flatCollection2 = collection2.stream().flatMap(Collection::stream).collect(Collectors.toList());
        return collection1.stream().filter(flatCollection2::contains).collect(Collectors.toList());

    }



}
